package org.goodsence.modernarrangedmarriage.controller;

import org.goodsence.api.TheRevoltApi;
import org.goodsence.framework.kafka.TopicName;
import org.goodsence.framework.rabbitmq.QueueName;
import org.goodsence.framework.websocket.WebSocketUtils;
import org.goodsence.frammework.common.utils.ResultUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author zty
 * @apiNote
 * @project_name advanced-cloud
 * @user tain
 * @create_at 2023/6/14 3:48
 * @create_vio IntelliJ IDEA
 */
@RestController
@RequestMapping("/test")
public class TestController {

    private final AmqpTemplate amqpTemplate;

    private final KafkaTemplate<String, Object> kafkaTemplate;

    private final StringRedisTemplate stringRedisTemplate;

    private final TheRevoltApi theRevoltApi;

    @Autowired
    public TestController(AmqpTemplate amqpTemplate, KafkaTemplate<String, Object> kafkaTemplate, StringRedisTemplate stringRedisTemplate, TheRevoltApi theRevoltApi) {
        this.amqpTemplate = amqpTemplate;
        this.kafkaTemplate = kafkaTemplate;
        this.stringRedisTemplate = stringRedisTemplate;
        this.theRevoltApi = theRevoltApi;
    }

    @GetMapping("/test")
    public ResponseEntity<String> test(){
        return ResultUtils.ok("hello world!");
    }

    @GetMapping("/api")
    public ResponseEntity<String> api(){
        String api = theRevoltApi.api();
        return ResultUtils.ok(api);
    }

    @GetMapping("/redis/set/{key}/{value}")
    public ResponseEntity<String> redisSet(@PathVariable String key, @PathVariable String value){
        stringRedisTemplate.opsForValue().set(key, value);
        return ResultUtils.ok();
    }

    @GetMapping("/redis/get/{key}")
    public ResponseEntity<String> redisGet(@PathVariable String key){
        String value = stringRedisTemplate.opsForValue().get(key);
        return ResultUtils.ok(value);
    }

    @GetMapping("/rabbitmq/send")
    public ResponseEntity<String> rabbitmqSend(){
        amqpTemplate.convertAndSend(QueueName.TEST, "rabbitMQ send one message!");
        return ResultUtils.ok();
    }

    @GetMapping("/kafka/send")
    public ResponseEntity<String> kafkaSend(){
        kafkaTemplate.send(TopicName.TEST, "kafkaMQ send one message!");
        return ResultUtils.ok();
    }

    @GetMapping("/websocket/send")
    public ResponseEntity<String> websocketSend(){
        try {
            WebSocketUtils.sendString("websocket send one message!");
        } catch (IOException e) {
            e.printStackTrace();
            return ResultUtils.error();
        }
        return ResultUtils.ok();
    }

}
